// ServicerInfo

// ---------------- Vue对象区
/**
 * 第一部分 信息区
 */
var vm_servicedListTable = new Vue({
    el: '#ServicerInfo',
    data: {
        servicer: new Object()
    },
    methods: {
        updateInfo: function () {
            this.$http.get("servicerInfo").then(function (value) {
                this.servicer = value.body;
            }, function (reason) {
                alert(reason.body);
                console.log(reason.body);
            })
        }
    }
});

// newRequestList
var vm_newRequestListTable = new Vue({
    el: '#newRequestListTable',
    data: {
        newRequestList: []
    },
    methods: {
        getNewRequestListTable: function () {
            this.$http.get("newRequestList").then(function (value) {
                this.newRequestList = value.body;
            }, function (reason) {
                alert(reason.body);
            })
        },
        joinChat: function (uid) {
            this.$http.post("joinChat", {uid: uid}).then(function (value) {
                vm_chatBox.getServiceUidList();
                alert(value.body)
            },function (reason) {
                alert(reason.body);
            })
        }

    }
})

/**
 * 功能盒子
 * funcBox
 */
var vm_funcBox = new Vue({
    el: '#funcBox',
    methods: {
        updateNewRequestList: function () {
            vm_newRequestListTable.getNewRequestListTable();
        }
    }
})

var vm_chatBox = new Vue({
    el: '#chatBox',
    data: {
        serviceUidList : [],
        nowChatUid: null,
        nowMsgList: [],
        sendMsg:{}
    },
    methods: {
        getServiceUidList: function () {
            this.$http.get("serviceUserList").then(function (value) { this.serviceUidList = value.body })
        },
        changeChat: function (uid) {
            this.nowChatUid = null;
            this.$http.get("sermsgList", {params: {uid: uid}}).then(function (value) {
                this.nowMsgList = value.body;
                this.nowChatUid = uid;
                setTimeout(this.getNewMsg, 1000);
             }, function (reason) { alert(reason) })
        },
        getNewMsg: function () {
            this.$http.get("serNewMsg", {params: {uid: this.nowChatUid}}).then(function (value) { this.nowMsgList.push(value.body); setTimeout(this.getNewMsg, 1000);},
                function (reason) {
                if (reason.body == "此用户没有正在通讯的会话。") {
                    this.nowChatUid = null;
                    this.nowMsgList.length=0;
                    setTimeout(this.getNewMsg, 100000);
                }
                else
                    setTimeout(this.getNewMsg, 1000);
            })
        },
        fsendMsg: function () {
            var msg = {};
            msg.value = this.sendMsg.value;
            msg.time = new Date();
            msg.type = 1;
            this.$http.post("serMsg?uid="+this.nowChatUid, msg).then(function (value) { this.nowMsgList.push(msg); });
        }
    }
});

/**
 * 聊天记录窗口
 */
// --------- 开局一血区
vm_servicedListTable.updateInfo();
vm_chatBox.getServiceUidList();
//////////////////////////////////////////////////////// --------- 循环获取数据区
setInterval(vm_funcBox.updateNewRequestList, 3000);
setInterval(vm_chatBox.getServiceUidList, 3000);
// 可以更改逻辑，用setTimeout()，以减轻服务器压力
// http://www.runoob.com/jsref/met-win-setinterval.html






























